package com.game.majiang.vo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import javax.sql.rowset.CachedRowSet;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;

public class PumpLogVo {

	int uid;
	int tableid;
	int tabletype;
	int gamechang;
	String allpump;
	String pingtaipump;
	
	int oneproxyid;
	String oneproxypump;
	String pingtaione="0";
	int twoproxyid;
	String twoproxypump;
	String pingtaitwo="0";
	int threeproxyid;
	String threeproxypump;
	String pingtaithree="0";
	int isbanker;
	String haomiao;
//	/**
//	 * 
//	 * @param uid   用户id
//	 * @param tableid  桌子号
//	 * @param tabletype 桌子类型
//	 * @param gamechang  游戏场
//	 * @param allpump   总抽水
//	 */
//	public PumpLogVo(int _uid,int _tableid,int _tabletype,int _gamechang,int _allpump){
//		this.uid = _uid;
//		this.tableid =_tableid;
//		this.tabletype = _tabletype;
//		this.gamechang = _gamechang;
//		this.allpump = String.valueOf(_allpump);
//		int[] pid =selectproxy();
//		double ap = Double.valueOf(_allpump).doubleValue();
//		double allpingtaipump = ap*(Integer.parseInt(DataManager.getInstance().systemValue("pingntaipump")))/100.0;
//		this.pingtaipump =  String.valueOf(allpingtaipump);
//		double alldailipump  = 	ap - allpingtaipump;
//		if(pid[0]==0){
//			this.pingtaipump = String.valueOf(_allpump);
//			this.oneproxyid = 0;
//			this.oneproxypump = "0";
//			this.twoproxyid = 0;
//			this.twoproxypump = "0";
//			this.threeproxyid = 0;
//			this.threeproxypump = "0";
//		}else{
//			if(pid[1]==0){
//				this.oneproxyid = pid[0];
//				this.oneproxypump =  String.valueOf(alldailipump);
//				BigDecimal big = new BigDecimal(this.oneproxypump);
//				BigDecimal setScale = big.setScale(2, RoundingMode.DOWN);
//				this.twoproxyid = 0;
//				this.twoproxypump = "0";
//				this.threeproxyid = 0;
//				this.threeproxypump = "0";
//				addproxygold(setScale,this.oneproxyid);
//			}else{
//				if(pid[2]==0){
//					this.oneproxyid = pid[0];
////					double pid1 = alldailipump*(Integer.parseInt(DataManager.getInstance().systemValue("oneproxypump")))/100.0;
//					double pid1 = alldailipump*(checkyiji(this.oneproxyid))/100.0;
//					this.oneproxypump =  String.valueOf(pid1);
//					BigDecimal big1 = new BigDecimal(this.oneproxypump);
//					BigDecimal setScale1 = big1.setScale(2, RoundingMode.DOWN);
//					addproxygold(setScale1,this.oneproxyid);
//					this.twoproxyid = pid[1];
//					this.twoproxypump =  String.valueOf(alldailipump-pid1);
//					BigDecimal big2 = new BigDecimal(this.twoproxypump);
//					BigDecimal setScale2 = big2.setScale(2, RoundingMode.DOWN);
//					addproxygold(setScale2,this.twoproxyid);
//					this.threeproxyid = 0;
//					this.threeproxypump = "0";
//				}else{
//					this.oneproxyid = pid[0];
//					double pid1 = alldailipump*(checkyiji(this.oneproxyid))/100.0;
//					this.oneproxypump =  String.valueOf(pid1);
//					BigDecimal big1 = new BigDecimal(this.oneproxypump);
//					BigDecimal setScale1 = big1.setScale(2, RoundingMode.DOWN);
//					addproxygold(setScale1,this.oneproxyid);
//					BigDecimal shen1=new BigDecimal(alldailipump).subtract(setScale1);
//					this.twoproxyid = pid[1];
////					double pid2 = alldailipump*(checkerji(this.twoproxyid))/100.0;
//					double pid2 = (shen1).doubleValue()*(checkerji(this.twoproxyid))/100.0;
//					BigDecimal big = new BigDecimal(pid2);
//					BigDecimal setScale = big.setScale(2, RoundingMode.DOWN);
//					pid2 = setScale.doubleValue();
//					this.twoproxypump =  String.valueOf(pid2);
//					addproxygold(setScale,this.twoproxyid);
//					this.threeproxyid = pid[2];
////					double pid3 = alldailipump*(Integer.parseInt(DataManager.getInstance().systemValue("threeproxypump")))/100.0;
//					double d1 = alldailipump-pid1;
//					double d2 = d1-pid2;
//					String str = String.valueOf(d2);
//					BigDecimal big2 = new BigDecimal(str);
//					BigDecimal setScale2 = big2.setScale(2, RoundingMode.DOWN);
//					this.threeproxypump = setScale2.toString();
//					addproxygold(setScale2,this.threeproxyid);
//				}
//			}
//
//		}
//		insertPumpLog();
//		
//	}
	
//	/**
//	 * 
//	 * @param uid   用户id
//	 * @param tableid  桌子号
//	 * @param tabletype 桌子类型
//	 * @param gamechang  游戏场
//	 * @param allpump   总抽水
//	 */
//	public PumpLogVo(int _uid,int _tableid,int _tabletype,int _gamechang,int _allpump,int _isbanker,String _haomiao){
//		this.uid = _uid;
//		this.tableid =_tableid;
//		this.tabletype = _tabletype;
//		this.gamechang = _gamechang;
//		this.allpump = String.valueOf(_allpump);
//		this.isbanker = _isbanker;
//		this.haomiao = _haomiao;
//		int[] pid =selectproxy();
//		double all = Double.valueOf(_allpump);
////		double ap = Double.valueOf(_allpump).doubleValue();
////		double allpingtaipump = ap*(Integer.parseInt(DataManager.getInstance().systemValue("pingntaipump")))/100.0;
////		this.pingtaipump =  String.valueOf(allpingtaipump);
////		double alldailipump  = 	ap - allpingtaipump;
//		if(_isbanker==0)
//		{
//			pid[0]=0;
//		}
//		if(pid[0]==0){
//			this.pingtaipump = String.valueOf(_allpump);
//			this.oneproxyid = 0;
//			this.oneproxypump = "0";
//			this.twoproxyid = 0;
//			this.twoproxypump = "0";
//			this.threeproxyid = 0;
//			this.threeproxypump = "0";
//		}else{
//			if(pid[1]==0){
//				double allpingtaipump = 0;
//				this.oneproxyid = pid[0];
//				ArrayList<Integer> pumpyiji =checkyiji(this.oneproxyid);
//				double pid1 = all*(pumpyiji.get(0))/100.0;
//				 String str1 =  String.valueOf(pid1);
//				BigDecimal big = new BigDecimal(str1);
//				BigDecimal setScale = big.setScale(2, RoundingMode.DOWN);
//				double done = setScale.doubleValue();
//				double done2 = done*allpingtaipump;
//				String allonepump = String.valueOf(done2);
//				BigDecimal one1 = new BigDecimal(allonepump);
//				BigDecimal onebig = one1.setScale(2, RoundingMode.DOWN);
//				this.pingtaione = onebig.toString();
//				BigDecimal oneshen1=setScale.subtract(onebig);
//				this.oneproxypump =oneshen1.toString();
//				addproxygold(oneshen1,this.oneproxyid);
//				
//				BigDecimal shen1=new BigDecimal(all).subtract(setScale);
////				double pingtai = pid1-setScale.doubleValue();
//				this.pingtaipump = shen1.toString();
//				this.twoproxyid = 0;
//				this.twoproxypump = "0";
//				this.threeproxyid = 0;
//				this.threeproxypump = "0";
//				
//			}else{
//				if(pid[2]==0){
//					this.oneproxyid = pid[0];
//					double allpingtaipump = 0;
//					ArrayList<Integer> pumpyiji =checkyiji(this.oneproxyid);
////					
//					double pid1 = all*(pumpyiji.get(0))/100.0;
//					  String str1 = String.valueOf(pid1);
//					BigDecimal big1 = new BigDecimal(str1);
//					BigDecimal setScale1 = big1.setScale(2, RoundingMode.DOWN);
////					String allonepump = setScale1.toString();
//					double done = setScale1.doubleValue();
//					double done2 = done*allpingtaipump;
//					String allonepump = String.valueOf(done2);
//					BigDecimal one1 = new BigDecimal(allonepump);
//					BigDecimal onebig = one1.setScale(2, RoundingMode.DOWN);
//					this.pingtaione = onebig.toString();
//					BigDecimal oneshen1=setScale1.subtract(onebig);
//					this.oneproxypump =oneshen1.toString();
//					addproxygold(oneshen1,this.oneproxyid);
//					
//					this.twoproxyid = pid[1];
//					double pid2 = all*(pumpyiji.get(1))/100.0;
//					String str2 = String.valueOf(pid2);
//					BigDecimal big2 = new BigDecimal(str2);
//					BigDecimal setScale2 = big2.setScale(2, RoundingMode.DOWN);
//					double dtwo = setScale2.doubleValue();
//					double dtwo2 = dtwo*allpingtaipump;
//					String alltwopump = String.valueOf(dtwo2);
//					BigDecimal two1 = new BigDecimal(alltwopump);
//					BigDecimal twobig = two1.setScale(2, RoundingMode.DOWN);
//					this.pingtaitwo = twobig.toString();
//					BigDecimal twoshen1=setScale2.subtract(twobig);
////					this.oneproxypump =oneshen1.toString();
//					this.twoproxypump =  twoshen1.toString();
//					addproxygold(twoshen1,this.twoproxyid);
//					
//					BigDecimal d =setScale1.add(setScale2);
////					double d = all-setScale1.doubleValue()-setScale2.doubleValue();
//					BigDecimal shen1=new BigDecimal(all).subtract(d);
////					String ping = String.valueOf(d);
//					this.pingtaipump = shen1.toString();
//					this.threeproxyid = 0;
//					this.threeproxypump = "0";
//				}else{
//					double allpingtaipump = 0;
//					this.oneproxyid = pid[0];
//					ArrayList<Integer> pumpyiji =checkyiji(this.oneproxyid);
//					double pid1 = all*(pumpyiji.get(0))/100.0;
//					  String str1 = String.valueOf(pid1);
//					BigDecimal big1 = new BigDecimal(str1);
//					BigDecimal setScale1 = big1.setScale(2, RoundingMode.DOWN);
//					double done = setScale1.doubleValue();
//					double done2 = done*allpingtaipump;
//					String allonepump = String.valueOf(done2);
//					BigDecimal one1 = new BigDecimal(allonepump);
//					BigDecimal onebig = one1.setScale(2, RoundingMode.DOWN);
//					this.pingtaione = onebig.toString();
//					BigDecimal oneshen1=setScale1.subtract(onebig);
//					this.oneproxypump =oneshen1.toString();
////					this.oneproxypump =setScale1.toString();
//					addproxygold(oneshen1,this.oneproxyid);
//					
//					this.twoproxyid = pid[1];
//					double pid2 = all*(pumpyiji.get(1))/100.0;
//					String str2 = String.valueOf(pid2);
//					BigDecimal big2 = new BigDecimal(str2);
//					BigDecimal setScale2 = big2.setScale(2, RoundingMode.DOWN);
//					double dtwo = setScale2.doubleValue();
//					double dtwo2 = dtwo*allpingtaipump;
//					String alltwopump = String.valueOf(dtwo2);
//					BigDecimal two1 = new BigDecimal(alltwopump);
//					BigDecimal twobig = two1.setScale(2, RoundingMode.DOWN);
//					this.pingtaitwo = twobig.toString();
//					BigDecimal twoshen1=setScale2.subtract(twobig);
////					this.oneproxypump =oneshen1.toString();
//					this.twoproxypump =  twoshen1.toString();
//					addproxygold(twoshen1,this.twoproxyid);
//					
//					this.threeproxyid = pid[2];
//					double pid3 = all*(pumpyiji.get(2))/100.0;
//					String str3 = String.valueOf(pid3);
//					BigDecimal big3 = new BigDecimal(str3);
//					BigDecimal setScale3 = big3.setScale(2, RoundingMode.DOWN);
//					double dthree = setScale3.doubleValue();
//					double dthree2 = dthree*allpingtaipump;
//					String allthreepump = String.valueOf(dthree2);
//					BigDecimal three1 = new BigDecimal(allthreepump);
//					BigDecimal threebig = three1.setScale(2, RoundingMode.DOWN);
//					this.pingtaithree = threebig.toString();
//					BigDecimal threeshen1=setScale3.subtract(threebig);
//					this.threeproxypump =  threeshen1.toString();
//					addproxygold(threeshen1,this.threeproxyid);
//					
//					BigDecimal dd =setScale1.add(setScale2).add(setScale3);
////					double dd = setScale1.doubleValue()+setScale2.doubleValue()+setScale2.doubleValue();
////					String strpin = String.valueOf(dd);
//					BigDecimal shen1=new BigDecimal(all).subtract(dd);
////					double d = all - Double.valueOf(strpin) ;
////					String ping = String.valueOf(d);
//					this.pingtaipump = shen1.toString();
//				}
//			}
//
//		}
//		insertPumpLog();
//		
//	}
	
	
	/**
	 * 
	 * @param uid   用户id
	 * @param tableid  桌子号
	 * @param tabletype 桌子类型
	 * @param gamechang  游戏场
	 * @param allpump   总抽水
	 */
	public PumpLogVo(int _uid,int _tableid,int _tabletype,int _gamechang,int _allpump,int _isbanker,String _haomiao){
		this.uid = _uid;
		this.tableid =_tableid;
		this.tabletype = _tabletype;
		this.gamechang = _gamechang;
		this.allpump = String.valueOf(_allpump);
		this.isbanker = _isbanker;
		this.haomiao = _haomiao;
		int[] pid =selectproxy();
		int all = _allpump;
		if(_isbanker==0)
		{
			pid[0]=0;
		}
		if(pid[0]==0){
			this.pingtaipump = String.valueOf(_allpump);
			this.oneproxyid = 0;
			this.oneproxypump = "0";
			this.twoproxyid = 0;
			this.twoproxypump = "0";
			this.threeproxyid = 0;
			this.threeproxypump = "0";
		}else{
			if(pid[1]==0){
				double allpingtaipump = 0;
				this.oneproxyid = pid[0];
				ArrayList<Integer> pumpyiji =checkyiji(this.oneproxyid);
//				double pid1 = all*(pumpyiji.get(0))/100.0;
				int pid1 = all*(pumpyiji.get(0))/100;
				
				this.pingtaione = "0";
			
				this.oneproxypump =pid1+"";
				addproxygold(pid1,this.oneproxyid);
				this.pingtaipump = String.valueOf(all-pid1);
				this.twoproxyid = 0;
				this.twoproxypump = "0";
				this.threeproxyid = 0;
				this.threeproxypump = "0";
				
			}else{
				if(pid[2]==0){
					this.oneproxyid = pid[0];
					double allpingtaipump = 0;
					ArrayList<Integer> pumpyiji =checkyiji(this.oneproxyid);
//					
					int pid1 = all*(pumpyiji.get(0))/100;
		
					this.pingtaione = "0";
				
					this.oneproxypump =pid1+"";
					addproxygold(pid1,this.oneproxyid);
					
					this.twoproxyid = pid[1];
					int pid2 = all*(pumpyiji.get(1))/100;
					if (pid2 > all - pid1) {
						pid2 = all - pid1;
					}
					this.pingtaitwo = "0";

					this.twoproxypump =  pid2+"";
					addproxygold(pid2,this.twoproxyid);

					this.pingtaipump = String.valueOf(all-pid1-pid2);
					this.threeproxyid = 0;
					this.threeproxypump = "0";
				}else{
					double allpingtaipump = 0;
					this.oneproxyid = pid[0];
					ArrayList<Integer> pumpyiji =checkyiji(this.oneproxyid);
					int pid1 = all*(pumpyiji.get(0))/100;
					
					this.pingtaione = "0";

					this.oneproxypump =pid1+"";

					addproxygold(pid1,this.oneproxyid);
					
					this.twoproxyid = pid[1];
					int pid2 = all*(pumpyiji.get(1))/100;
					if (pid2 > all - pid1) {
						pid2 = all - pid1;
					}
					this.pingtaitwo = "0";

					this.twoproxypump =  pid2+"";
					addproxygold(pid2,this.twoproxyid);
					
					this.threeproxyid = pid[2];
					int pid3 = all*(pumpyiji.get(2))/100;
					if (pid3 > all - pid1-pid2) {
						pid3 = all - pid1 - pid2;
					}
					this.pingtaithree = "0";
					
					this.threeproxypump =  pid3+"";
					addproxygold(pid3,this.threeproxyid);

					this.pingtaipump = String.valueOf(all - pid1 - pid2 - pid3);
				}
			}

		}
		insertPumpLog();
		
	}
	
	
	public void insertPumpLog()
	{
		String cmdText = "insert into pump_log(uid,tableid,tabletype,gamechang,allpump,pingtaipump,oneproxyid,oneproxypump,"
				+ "twoproxyid,twoproxypump,threeproxyid,threeproxypump,isbanker,pingtaione,pingtaitwo,pingtaithree,haomiao)"
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, tableid, ParameterType.Input);
		Parameter p3 = new Parameter(Types.INTEGER, tabletype, ParameterType.Input);
		Parameter p4 = new Parameter(Types.INTEGER, gamechang, ParameterType.Input);
		Parameter p5 = new Parameter(Types.VARCHAR, allpump, ParameterType.Input);
		Parameter p6 = new Parameter(Types.VARCHAR, pingtaipump, ParameterType.Input);
		Parameter p7 = new Parameter(Types.INTEGER, oneproxyid, ParameterType.Input);
		Parameter p8 = new Parameter(Types.VARCHAR, oneproxypump, ParameterType.Input);
		Parameter p9 = new Parameter(Types.INTEGER, twoproxyid, ParameterType.Input);
		Parameter p10 = new Parameter(Types.VARCHAR, twoproxypump, ParameterType.Input);
		Parameter p11 = new Parameter(Types.INTEGER, threeproxyid, ParameterType.Input);
		Parameter p12 = new Parameter(Types.VARCHAR, threeproxypump, ParameterType.Input);
		Parameter p13 = new Parameter(Types.INTEGER, isbanker, ParameterType.Input);
		Parameter p14 = new Parameter(Types.VARCHAR, pingtaione, ParameterType.Input);
		Parameter p15 = new Parameter(Types.VARCHAR, pingtaitwo, ParameterType.Input);
		Parameter p16 = new Parameter(Types.VARCHAR, pingtaithree, ParameterType.Input);
		Parameter p17 = new Parameter(Types.VARCHAR, haomiao, ParameterType.Input);
		SqlHelperwrite.execute(cmdText,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17);
	}
	
	public int[] selectproxy(){
		String cmdText ="SELECT u2.pid1 AS pid1,u2.pid2 AS pid2 , p2.pid AS pid3 FROM (SELECT u1.pid1 AS pid1,p1.pid AS pid2 FROM (SELECT pid AS pid1 FROM users  WHERE id=?) u1 LEFT JOIN  proxy  p1  ON u1.pid1=p1.id  ) u2 LEFT JOIN proxy p2 ON u2.pid2=p2.id";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		CachedRowSet rs=  SqlHelper.getResultSet(cmdText, p1);
		int[]  pid = new int[3];
		try {
			while (rs.next()) {
				pid[0]=rs.getInt(1);
				pid[1]=rs.getInt(2);
				pid[2]=rs.getInt(3);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean bool =checkProxy(pid[0]);
		if(!bool){
			pid[0]=0;
			pid[1]=0;
			pid[2]=0;
		}
		return pid;
		
	}
	public void addproxygold(BigDecimal gold,int pid){
		String cmdText ="UPDATE  proxy SET integral = integral+? WHERE id = ?";
		Parameter p1 = new Parameter(Types.DECIMAL, gold, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
		SqlHelperwrite.execute(cmdText, p1,p2);
		
	}
	public void addproxygold(int gold,int pid){
		String cmdText ="UPDATE  proxy SET allhongli = allhongli+?,yuhongli=yuhongli+? WHERE id = ?";
		Parameter p1 = new Parameter(Types.INTEGER, gold, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, gold, ParameterType.Input);
		Parameter p3 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
		SqlHelperwrite.execute(cmdText, p1,p2,p3);
		
	}
	/**
	 * 查代理一级抽水
	 * @param gold
	 * @param pid
	 */
	public ArrayList<Integer> checkyiji(int pid){
		ArrayList<Integer> list = new ArrayList<Integer>();
		String cmdText ="SELECT yiji,erji,sanji FROM proxy WHERE id = ?";
		Parameter p1 = new Parameter(Types.INTEGER, pid, ParameterType.Input);

		CachedRowSet rs =SqlHelper.getResultSet(cmdText, p1); 
		try {
			while (rs.next()) {
				list.add(rs.getInt(1));
				list.add(rs.getInt(2));
				list.add(rs.getInt(3));
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 查代理二级抽水
	 * @param gold
	 * @param pid
	 */
	public int checkerji(int pid){
		String cmdText ="SELECT erji FROM proxy WHERE id = ?";
		Parameter p1 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
		Object obj = SqlHelper.execScalar(cmdText, p1);
		if(obj==null){
			return 0;
		}else{
			Integer i =(Integer)obj;
			if(i>0){
				return i;
			}else{
				return 0;
			}
			
		}
		
	}
	/**
	 * 查代理二级抽水
	 * @param gold
	 * @param pid
	 */
	public int checksanji(int pid){
		String cmdText ="SELECT sanji FROM proxy WHERE id = ?";
		Parameter p1 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
		Object obj = SqlHelper.execScalar(cmdText, p1);
		if(obj==null){
			return 0;
		}else{
			Integer i =(Integer)obj;
			if(i>0){
				return i;
			}else{
				return 0;
			}
			
		}
		
	}
	/**
	 *查询一级代理是否存在
	 */
	public static boolean checkProxy(int uid ) {

		String sql = "select id from proxy where id=? ";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);

		Object obj  = 	SqlHelper.execScalar(sql,p1);
		if(obj == null){
			return false;
		}else{
			return (int)obj>0;
		}
			
		
			

	}
}
